package com.peixunp.controller;

import java.io.File;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.peixunp.bean.Information;
import com.peixunp.bean.Organizations;
import com.peixunp.bean.RemarkTemplate;
import com.peixunp.service.InformationService;
import com.peixunp.service.OrganizationsService;
import com.peixunp.service.PlaceService;
import com.peixunp.service.RemarkService;
import com.peixunp.service.RemarkTemplateService;

@Controller
@RequestMapping("finaler")
public class FinalerController {

	@Autowired
	private OrganizationsService organizationsService;

	@Autowired
	private InformationService informationService;

	@Autowired
	private RemarkService remarkService;

	@Autowired
	private RemarkTemplateService remarkTemplateService;
	
	@Autowired
	private PlaceService placeService;

	@Autowired
	private ServletContext servletContext;
	
	@RequestMapping()
	public String index() {
		return "/finaler/login";
	}
	
	@RequestMapping( "/login" )
	public String login(HttpServletRequest request) {
		if( request.getParameter("finalername").equalsIgnoreCase("finaler")&&
				request.getParameter("finalerpsw").equalsIgnoreCase("1982o7o9") ){
			request.getSession().setAttribute("finaler", "finaler");
			return "/finaler/menu";
		}else{
			return "/finaler/login";
		}
	}

	@RequestMapping( "/createIndexJsp" )
	public String createIndexJsp(HttpServletRequest request) throws Exception {
		String file_real_path = servletContext.getRealPath("WEB-INF"+File.separator+"jsp"+File.separator+"school");
		organizationsService.autoCreateWantSum(file_real_path);//生成首页报读人数
		remarkService.autoCreate5HighCoin(file_real_path);//首页5个最高分
		remarkService.autoCreate5MostRemark(file_real_path);//首页5个最多评
		
		file_real_path = servletContext.getRealPath("WEB-INF"+File.separator+"jsp"+File.separator+"remark");
		remarkService.autoCreate50newRemark(file_real_path);
		request.setAttribute("result", "生成首页数据成功.");
		return "/finaler/menu";
	}
	
	@RequestMapping( "/createCityList" )
	public String createCityList(HttpServletRequest request) throws Exception {
		if(request.getSession().getAttribute("finaler").toString().equals("finaler")==false){
			return "/finaler/login";
		}
		String city_file_real_path = servletContext.getRealPath("WEB-INF"+File.separator+"jsp"+File.separator+"school");
		boolean result = placeService.createCityList(city_file_real_path);
		if(result){
			request.setAttribute("result", "生成  城市(学校) 列表   成功.");
		}else{
			request.setAttribute("result", "注意：生成  城市(学校) 列表   失败！！！");
		}
		return "/finaler/menu";
	}

	@RequestMapping( "/auditOrg" )
	public String auditOrg(HttpServletRequest request) throws Exception {
		List<Organizations> organizationsList = organizationsService.getAuditList();
		request.setAttribute("organizationsList",organizationsList);
		return "/finaler/auditOrg";
	}

	@RequestMapping( "/modiOrg_{orgId}" )
	public String modiOrg(@PathVariable int orgId,HttpServletRequest request) throws Exception {
		Organizations organizations  = organizationsService.findByKey(orgId);
		request.setAttribute("organizations",organizations);
		return "/finaler/modiOrg";
	}
	
	@RequestMapping( "/saveOrg_{orgId}" )
	public String saveOrg(@PathVariable int orgId,HttpServletRequest request) throws Exception {
		Organizations org  = new Organizations();
		org.setId(orgId);
		org.setAuditFlag(1);
		org.setCity(Integer.parseInt(request.getParameter("city")));
		org.setOrgaddr(request.getParameter("orgaddr"));
		org.setOrgcontent(request.getParameter("orgcontent"));
		org.setOrgname(request.getParameter("orgname"));
		org.setOrgshortname(request.getParameter("orgshortname"));
		org.setOrgtel(request.getParameter("orgtel"));
		org.setWhencreate(request.getParameter("whencreate"));
		org.setHit(Integer.parseInt(request.getParameter("hit")));
		org.setWant(Integer.parseInt(request.getParameter("want")));
		org.setOnce(Integer.parseInt(request.getParameter("once")));
		organizationsService.update(org);
		request.setAttribute("result", "学校修改成功!!!");
		return "/finaler/menu";
	}

	@RequestMapping( "/delOrg_{orgId}" )
	public String delOrg(@PathVariable int orgId,HttpServletRequest request) throws Exception {
		Organizations org  = new Organizations();
		org.setId(orgId);
		organizationsService.delete(org);
		request.setAttribute("result", "学校删除成功!!!");
		return "/finaler/menu";
	}

	@RequestMapping( "/auditInfo" )
	public String auditInfo(HttpServletRequest request) throws Exception {
		List<Information> informationList = informationService.getAuditList();
		request.setAttribute("informationList", informationList);
		return "/finaler/auditInfo";
	}
	
	@RequestMapping( "/modiInfo_{infoId}" )
	public String modiInfo(@PathVariable int infoId,HttpServletRequest request) throws Exception {
		Information information = informationService.findByKey(infoId);
		information.setAuditFlag(1);
		informationService.update(information);
		request.setAttribute("result", "信息审核通过!!!");
		return "/finaler/menu";
	}

	@RequestMapping( "/delInfo_{infoId}" )
	public String delInfo(@PathVariable int infoId,HttpServletRequest request) throws Exception {
		Information information  = new Information();
		information.setId(infoId);
		informationService.delete(information);
		request.setAttribute("result", "信息删除成功!!!");
		return "/finaler/menu";
	}

	@RequestMapping( "/auditRMTemp" )
	public String auditRMTemp(HttpServletRequest request) throws Exception {
		List<RemarkTemplate> remarkTemplateList = remarkTemplateService.getAll();
		request.setAttribute("remarkTemplateList", remarkTemplateList);
		return "/finaler/auditRMTemp";
	}
	
	@RequestMapping( "/delRMTemp_{rmtpId}" )
	public String delRMTemp(@PathVariable int rmtpId,HttpServletRequest request) throws Exception {
		RemarkTemplate rmtp = new RemarkTemplate();
		rmtp.setId(rmtpId);
		remarkTemplateService.delete(rmtp);
		return "redirect:/finaler/auditRMTemp";
	}

	@RequestMapping( "/addRMTemp" )
	public String delRMTemp(HttpServletRequest request) throws Exception {
		RemarkTemplate rmtp = new RemarkTemplate();
		rmtp.setCoin(Integer.parseInt(request.getParameter("coin")));
		rmtp.setRmcontent(request.getParameter("rmcontent"));
		remarkTemplateService.insert(rmtp);
		return "redirect:/finaler/auditRMTemp";
	}
}
